package com.gsbelarus.gedemin.salary.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.MenuItem;

import com.gsbelarus.gedemin.lib.sync.protocol.SyncProtocol;
import com.gsbelarus.gedemin.lib.sync.protocol.entity.SyncServiceStatus;
import com.gsbelarus.gedemin.lib.ui.activity.SyncActivityModel;
import com.gsbelarus.gedemin.salary.R;
import com.gsbelarus.gedemin.salary.fragment.DrawerPersonInfoFragment;
import com.gsbelarus.gedemin.salary.util.GCMHelper;

public class DrawerPersonInfo extends SyncActivityModel {

    private DrawerPersonInfoFragment fragment;

    /**
     * @return id View для отрисовки в активити
     */
    @Override
    public int getViewResource() {
        return R.layout.activity_fragment;
    }

    @Override
    public void onReceiveSyncStatus(SyncServiceStatus syncServiceStatus, SyncStatusDialog syncStatusDialog) {
        super.onReceiveSyncStatus(syncServiceStatus, syncStatusDialog);
        syncStatusDialog.show(syncServiceStatus);
        if (syncServiceStatus.getTypeOfStatus() == SyncServiceStatus.TypeOfStatus.FINISH_SYNC &&
                syncServiceStatus.getStatus() != SyncProtocol.SyncStatus.NO_INTERNET_CONNECTION)
            fragment.reloadFragmentData();
    }

    /**
     * вызывается в методе onCreate(Bundle)
     *
     * @param arguments bundle с агрументами
     */
    @Override
    public void onCreateActivity(Bundle arguments) {
        super.onCreateActivity(arguments);

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("");
        }
        getToolbar().setBackgroundResource(R.drawable.shadow_toolbar_background);
        showContentPaddingTop(false);
        showElevationToolbar(false);
        findViewById(com.gsbelarus.gedemin.lib.R.id.actvt_fits_system_windows).setFitsSystemWindows(false);
        getToolbar().setFitsSystemWindows(true);

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (fragment != null)
            fragmentTransaction.remove(fragment);
        fragment = new DrawerPersonInfoFragment();
        int resourceDrawable = getIntent().getIntExtra(DrawerPersonInfoFragment.SAVE_RESOURCE_DRAWABLE, 0);
        int resourceDrawableAccount = getIntent().getIntExtra(DrawerPersonInfoFragment.SAVE_RESOURCE_DRAWABLE_ACCOUNT, 0);
        if (resourceDrawable != 0 && resourceDrawableAccount != 0) {
            Bundle bundle = new Bundle();
            bundle.putInt(DrawerPersonInfoFragment.SAVE_RESOURCE_DRAWABLE, resourceDrawable);
            bundle.putInt(DrawerPersonInfoFragment.SAVE_RESOURCE_DRAWABLE_ACCOUNT, resourceDrawableAccount);
            fragment.setArguments(bundle);
        }
        fragmentTransaction.add(R.id.frame_content, fragment);
        fragmentTransaction.commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        new GCMHelper(this).checkPlayServices(this);
    }
}
